<?php
/**
 * Message system for all applications
 *
 */
class Message{


    /**
     * Add messages into message system
     *
     * @param string $message
     * @param string $type
     * @param boolean $repeat
     * @return array $messages
     */
    public static function setMessage($message = NULL, $type = 'status', $repeat = TRUE){

      
        
        if($message){
            if(!isset($_SESSION['messages']))
            $_SESSION['messages'] = array();

            if(!isset($_SESSION['messages'][$type]))
            $_SESSION['messages'][$type] = array();

            if($repeat || !in_array($message, $_SESSION['messages'][$type]))
            $_SESSION['messages'][$type][] = $message;
        }

        return isset($_SESSION['messages'])? $_SESSION['messages'] : null;
    }


    /**
     * Retrieve messages from message system
     *
     * @param string $type
     * @param boolean $clear_queue
     * @return array $messages
     */
    public static function getMessages($type = NULL, $clear_queue = TRUE) {
         
      
        
        if($messages = self::setMessage()){
            if($type){
                if($clear_queue)
                unset($_SESSION['messages'][$type]);

                if(isset($messages[$type]))
                return array($type => $messages[$type]);
            }
            else{
                if($clear_queue)
                unset($_SESSION['messages']);
            }
            return $messages;
        }
        
        return array();
    }

}

